package com.example.testproj.http;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * @des:
 * @author: 芮勤
 * @date: 2019/6/23 11:50
 * @see {@link }
 */
public class HttpResponseEntry extends HttpEntry {
    private String reason;
    private int state;


    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }



    public String getHeaderMessage() {
        StringBuffer sb = new StringBuffer();
        sb.append(httpVersion).append(" ").append(state).append(" ").append(reason).append(splitString);
        for (Map.Entry<String, String> bean :
                getHeaders()) {
            sb.append(bean.getKey()).append(":").append(bean.getValue()).append(splitString);
        }
        return sb.toString();
    }

    public static class Builder{
        private HttpResponseEntry httpResponseEntry;

        public HttpResponseEntry builder(){
            httpResponseEntry=new HttpResponseEntry();
            httpResponseEntry.setReason("OK");
            httpResponseEntry.setState(200);
            httpResponseEntry.setHttpVersion("HTTP/1.1");
            httpResponseEntry.addHeader("Last-Modified",getRFCTime(System.currentTimeMillis()+60*1000));
            httpResponseEntry.addHeader("Date",getRFCTime(System.currentTimeMillis()));
            httpResponseEntry.addHeader("Server","Lanlengran-HTTP/0.1");
            httpResponseEntry.addHeader("Content-Type","text/html");
            return httpResponseEntry;
        }

        public String getRFCTime(long time){
            SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            sdf3.setTimeZone(TimeZone.getTimeZone("GMT"));
            String rfc1123_3 = sdf3.format(new Date(time));
            return rfc1123_3;
        }
    }
}
